package com.siwei.mes.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.siwei.mes.common.query.PageQuery;
import com.siwei.mes.common.util.ScriptUtils;
import com.siwei.mes.entity.CheckBatchConfig;
import com.siwei.mes.enums.CheckEnum;
import com.siwei.mes.mapper.CheckBatchConfigMapper;
import com.siwei.mes.mapper.ExperimentCheckItemorderMapper;
import com.siwei.mes.service.CheckBatchConfigService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

import static com.siwei.mes.common.exception.handler.MessageHandler.assertTrue;

/**
 * 批检指标配置(CheckBatchConfig)表服务实现类
 *
 * @author linzi
 * @since 2023-11-25 22:22:18
 */
@Service
public class CheckBatchConfigServiceImpl implements CheckBatchConfigService {
    @Resource
    private CheckBatchConfigMapper checkBatchConfigMapper;
    @Resource
    private ExperimentCheckItemorderMapper experimentCheckItemorderMapper;

    /**
     * 组装指标用于后面通过js去执行
     *
     * @param entity
     */
    private void payloadFormula(CheckBatchConfig entity) {
        /**组装合格指标 用于后面通过js去执行*/
        String qualifiedFormula = ScriptUtils.makeFormula(entity.getQualifiedCheckOneType(), entity.getQualifiedCheckOneValue(), entity.getQualifiedCheckTwoType(), entity.getQualifiedCheckTwoValue());
        entity.setQualifiedCheckText(qualifiedFormula);
        /**组装让步指标  用于后面通过js去执行*/
        String unQualifiedFormula = ScriptUtils.makeFormula(entity.getUnQualifiedCheckOneType(), entity.getUnQualifiedCheckOneValue(), entity.getUnQualifiedCheckTwoType(), entity.getUnQualifiedCheckTwoValue());
        entity.setUnQualifiedCheckText(unQualifiedFormula);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long create(CheckBatchConfig entity) {
        entity.init(true);
        /**组装指标 用于后面通过js去执行*/
        this.payloadFormula(entity);
        checkBatchConfigMapper.insertSelective(entity);

        /**只要操作了就清空*/
        experimentCheckItemorderMapper.delectByCheckType(CheckEnum.BATCH.getCode());
        return entity.getId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(CheckBatchConfig entity) {
        CheckBatchConfig checkBatchConfig = checkBatchConfigMapper.selectByPrimaryKey(entity.getId());
        assertTrue(checkBatchConfig == null, "批检指标配置信息不存在");
        /**组装指标 用于后面通过js去执行*/
        this.payloadFormula(entity);
        entity.init(false);
        checkBatchConfigMapper.updateByPrimaryKeySelective(entity);

        /**修改允许为null 指标2 相关数据*/
        checkBatchConfigMapper.updateIsNullInfo(entity);

        /**只要操作了就清空*/
        experimentCheckItemorderMapper.delectByCheckType(CheckEnum.BATCH.getCode());
    }

    @Override
    public void delete(Long id) {
        CheckBatchConfig checkBatchConfig = checkBatchConfigMapper.selectByPrimaryKey(id);
        assertTrue(checkBatchConfig == null, "批检指标配置信息不存在");
        checkBatchConfigMapper.deleteByPrimaryKey(id);
    }

    @Override
    public PageInfo<CheckBatchConfig> getPage(PageQuery<CheckBatchConfig, CheckBatchConfig> query) {
        CheckBatchConfig searchParam = query.getParams();
        return PageHelper.startPage(query.getPageNum(), query.getPageSize())
                .doSelectPageInfo(() -> checkBatchConfigMapper.getList(searchParam));
    }

    @Override
    public CheckBatchConfig loadById(Long id) {
        return checkBatchConfigMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<CheckBatchConfig> getAll(CheckBatchConfig entity) {
        return checkBatchConfigMapper.getList(entity);
    }

    @Override
    public void updateByPrimaryKeySelective(CheckBatchConfig entity) {
        CheckBatchConfig checkBatchConfig = checkBatchConfigMapper.selectByPrimaryKey(entity.getId());
        assertTrue(checkBatchConfig == null, "批检指标配置信息不存在");
        checkBatchConfigMapper.updateByPrimaryKeySelective(entity.init(false));
    }
}
